package cn.kimming.system.controller;

import cn.kimming.common.dto.RoomAddDTO;
import cn.kimming.common.dto.RoomCleanStatusDTO;
import cn.kimming.common.dto.RoomUpdateDTO;
import cn.kimming.common.pojo.Room;
import cn.kimming.common.util.Result;
import cn.kimming.common.vo.RoomInFoVO;
import cn.kimming.common.vo.RoomVO;
import cn.kimming.service.IRoomService;
import cn.kimming.service.IRoomTypeService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 刘铭轩
 * date 2021-05-23
 */
@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IRoomTypeService roomTypeService;
    @GetMapping("/page/{page}")
    public Result findAll(@PathVariable int page, Integer roomNum) {
        PageInfo<RoomVO> info = roomService.findAllByPage(page, roomNum);
        return new Result(true, "查询成功").addData("info", info);
    }
    @GetMapping("/info/page/{page}")
    public Result findInfo(@PathVariable int page, Integer roomNum) {
        PageInfo<RoomInFoVO> info = roomService.findInfo(page, roomNum);
        return new Result(true, "查询成功").addData("info", info);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id) {
        RoomVO room = roomService.findById(id);
        return new Result(true, "查询成功").addData("room", room);
    }

    @PostMapping
    public Result save(@Valid @RequestBody RoomAddDTO dto) {
        Room bo = new Room();
        BeanUtils.copyProperties(dto, bo);
        roomService.save(bo);
        return new Result(true, "新增成功");
    }

    @PutMapping
    public Result update(@Valid @RequestBody RoomUpdateDTO dto) {
        Room bo = new Room();
        BeanUtils.copyProperties(dto, bo);
        roomService.update(bo);
        return new Result(true, "更新成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        roomService.deleteById(id);
        return new Result(true, "删除成功");
    }

    @GetMapping("/findRoomFilter")
    public Result findRoomFilter() {
        List<JSONObject> buildingCodeFilter = roomService.find4BuildingCodeFilter();
        List<JSONObject> roomTypeFilter = roomTypeService.find4RoomTypeFilter();
        return new Result(true, "查询成功").addData("buildingCodeFilter",buildingCodeFilter).addData("roomTypeFilter", roomTypeFilter);
    }

    @PutMapping("/cleanStatus")
    public Result updateCleanStatus(@Valid @RequestBody RoomCleanStatusDTO dto) {
        Room bo = new Room();
        BeanUtils.copyProperties(dto, bo);
        roomService.update(bo);
        return new Result(true, "修改清洁状态成功");
    }

}
